
"""simple crawler.

发送请求 获取html
找到 需要的部分  DIV
data cleaning.
show.
不贪婪
"""

from urllib import request
import re


class Spider():
    """simple crawler."""

    url = 'https://www.panda.tv/search?kw=lol&pdt=1.18.psbar-s.0.3m8sjgj82gc'
    root_pattern = '<div class="video-info">([\d\D]*?)</div>'
    name_spider = '<span class="video-nickname">([\d\D]*?)</span>'
    hot_spider = '<span class="video-number">([\d\D]*?)</span>'

    def __get_html(self):
        """"climbing."""
        r = request.urlopen(Spider.url)
        html = r.read()
        html = str(html, encoding='utf-8')
        return html

    def __get_target(self, html):
        targets = re.findall(Spider.root_pattern, html)
        results = []
        for x in targets:
            name = re.findall(Spider.name_spider, x)
            hot = re.findall(Spider.hot_spider, x)
            result = {'name': name, 'hot': hot}
            results.append(result)
        return results

    def __cleaning(self, results):

        def clean(result):
            """clean."""
            return {
                'name': result['name'][0].strip(),
                'hot': result['hot'][0].strip()
            }
        return map(clean, results)

        # PEP-8不建议使用lambda
        # clean = lambda result: {
        #     'name': result['name'][0].strip(),
        #     'hot': result['hot'][0].strip()
        # }

    def __sort_seed(self, result):
        r = re.findall('\d*', result['hot'])
        # 正则表达式后 得到数组 []
        number = float(r[0])
        if '万' in result['hot']:
            number *= 10000
        return number

    def __sort(self, results):
        sort = sorted(results, key=self.__sort_seed, reverse=True)
        return sort

    def __show(self, results):
        for rank in range(0, len(results)):
            print(
                str(rank + 1) + "    " +
                results[rank]['name'] + "   " +
                results[rank]['hot']
            )

    def start(self):
        """start."""
        html = self.__get_html()
        results = self.__get_target(html)
        results = self.__cleaning(results)
        results = self.__sort(results)
        self.__show(results)

spider = Spider()
spider.start()
